package com.zjx.activiti.controller;

import com.zjx.activiti.common.Result;
import com.zjx.activiti.model.Copy;
import com.zjx.activiti.service.CopyService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Copy)表控制层
 *
 * @author makejava
 * @since 2021-08-31 15:04:41
 */
@RestController
@RequestMapping("copy")
public class CopyController {
    /**
     * 服务对象
     */
    @Resource
    private CopyService copyService;

    @Autowired
    private HistoryService historyService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Copy selectOne(Integer id) {
        return this.copyService.queryById(id);
    }

    @PostMapping("insert")
    public Object insert(@RequestBody Copy copy) {
        return this.copyService.insert(copy);
    }

    @GetMapping("myCopy")
    public Object getMyCopy(@RequestParam String userId) {
        //获取抄送列表
        List<Copy> list = copyService.queryByUserId(userId);
        //获取抄送的流程信息
        if(Objects.isNull(list) || list.size() == 0){
            return new Result<>(new ArrayList());
        }

        List<HistoricProcessInstance> processInstances = historyService.createHistoricProcessInstanceQuery().
                processInstanceIds(list.stream().map(Copy::getProcinstid).collect(Collectors.toSet())).list();
        return new Result<>(processInstances);
    }

}
